package com.yeyks.controller.other;

import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.annotation.NeedToken;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.constants.AccountType;
import com.yeyks.common.em.ResponseStatus;
import com.yeyks.common.exception.PermissionInvalidException;
import com.yeyks.commonReference.service.account.AccountInfoService;
import com.yeyks.config.dal.dao.MessageMapper;
import com.yeyks.config.dal.domain.Message;
import com.yeyks.web.AccountUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.InvalidParameterException;
import java.util.List;
import java.util.Map;

@Api(tags = "短信通知模块")
@RestController
@RequestMapping(value = "/ajax/msg")
public class MessageController {

    @Autowired
    AccountInfoService accountService;

    @Autowired
    MessageMapper messageMapper;

    /**
     * 查看入住通知消息
     *
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @NeedToken
    @RequestMapping(value = "/list", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<?> list(
            @RequestParam(name = "pageNum", defaultValue = "0", required = true) Integer pageNum,
            @RequestParam(name = "pageSize", defaultValue = "0", required = true) Integer pageSize
    ) {
        Integer offset = null, maxrow = null;
        if (pageNum != 0 && pageSize != 0) {
            offset = (pageNum - 1) * pageSize;
            maxrow = pageSize;
        }
        AjaxResponseBody<Map> responseBody = AjaxResponseBody.getMapAjaxResponseBody();
        try {
            AccountInfo accountInfo = AccountUtils.getLoginUser();

            List<Message> messageList = null;
            Integer count = null;
            if (accountInfo.getType() == AccountType.User) {
                throw new PermissionInvalidException();
            } else {
                messageList = messageMapper.selectByUserTypePage(null, "AB", offset, maxrow);
                count = messageMapper.selectByUserTypePageCount(null, "AB");
            }

            responseBody.getBody().put("count", count);
            responseBody.getBody().put("list", messageList);

            responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
            responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
        } catch (InvalidParameterException e) {
            responseBody.setCode(ResponseStatus.RS_FAIL_INVALID_PERMISSION.getCode());
            responseBody.setMsg(e.getMessage());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL.getMsg());
        }

        return ResponseEntity.ok(responseBody);

    }
}